package com.github.jinatonic.confetti.sample.slice;

import com.github.jinatonic.confetti.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private static final ConfettiSample[] SAMPLES = {
            new ConfettiSample(
                    ResourceTable.String_falling_confetti_from_top,
                    new FallingConfettiFromTopAbilitySlice()
            ),
            new ConfettiSample(
                    ResourceTable.String_falling_confetti_from_point,
                    new FallingConfettiFromPointAbilitySlice()
            ),
            new ConfettiSample(
                    ResourceTable.String_explosion_confetti,
                    new ExplosionAbilitySlice()
            ),
            new ConfettiSample(
                    ResourceTable.String_shimmering_confetti,
                    new ShimmeringAbilitySlice()
            ),
            new ConfettiSample(
                    ResourceTable.String_ice_flakes_with_touch,
                    new FallingWithTouchAbilitySlice()
            ),
            new ConfettiSample(
                    ResourceTable.String_ice_flakes_with_listener,
                    new FallingConfettiWithListenerAbilitySlice()
            ),
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list);
        listContainer.setItemProvider(new MainProvider(SAMPLES, this.getContext()));
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                present(SAMPLES[i].targetActivityClass, new Intent());
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private static class ConfettiSample {
        final int nameResId;
        final AbilitySlice targetActivityClass;

        private ConfettiSample(int nameResId, AbilitySlice targetActivityClass) {
            this.nameResId = nameResId;
            this.targetActivityClass = targetActivityClass;
        }
    }
    private static class MainProvider extends BaseItemProvider{

        private ConfettiSample[] list;
        private Context context;
        MainProvider(ConfettiSample[] list, Context context){
            this.list = list;
            this.context = context;
        }
        @Override
        public int getCount() {
            return list.length;
        }

        @Override
        public Object getItem(int i) {
            return list[i];
        }

        @Override
        public long getItemId(int i) {
            return list[i].hashCode();
        }

        @Override
        public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
            Text text = (Text) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_confetti_sample,componentContainer,false);
            ConfettiSample confettiSample = list[i];
            text.setText(confettiSample.nameResId);

            return text;
        }
    }
}
